package cn.jhc.Adapter;

import java.util.List;

import com.example.shilehui.R;

import cn.jhc.bean.City;
import cn.jhc.layout.SearchCityListView;
import cn.jhc.service.SearchService;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchCityAdapter extends BaseAdapter {
	
	private Context _context;
	private List<City> _list;
	private ViewHolder _viewHolder;
	private SearchCityListView _searchCityListView;
	private int _position;
	private ImageView _imageView;
	
	public SearchCityAdapter(Context context,List<City> list,SearchCityListView listView){
		
		_context=context;
		_list=list;
		_searchCityListView=listView;
		_searchCityListView.setOnItemClickListener(new onListViewItemClickListener());
		
		_position=SearchService.getInstance().getCityPosition();
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return _list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		if(convertView==null){
			LayoutInflater layoutInflater=(LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView=layoutInflater.inflate(R.layout.search_district_item,null);
			_viewHolder=new ViewHolder();
			
			_viewHolder.textView=(TextView)convertView.findViewById(R.id.search_district_item_textview);
			_viewHolder.imageView=(ImageView)convertView.findViewById(R.id.search_district_item_imageview);
			convertView.setTag(_viewHolder);
		}else{
			
			_viewHolder=(ViewHolder)convertView.getTag();
			
		}
		
		if(_position==position){
			
			_viewHolder.imageView.setVisibility(View.VISIBLE);
			
		}else{
			_viewHolder.imageView.setVisibility(View.GONE);
		}
		
		
		_viewHolder.textView.setText(_list.get(position).getCityName());
		_viewHolder.textView.setTextColor(Color.BLACK);
		
		return convertView;
	}
	
	private class ViewHolder{
		
		TextView textView;
		ImageView imageView;
		
	}
	
	private class onListViewItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			
			
			_imageView=SearchService.getInstance().getAllCityImageView();
			_imageView.setVisibility(View.GONE);
			
			SearchService.getInstance().SetCityPosition(position);
			_position=SearchService.getInstance().getCityPosition();
			
			SearchService.getInstance().setCity(_list.get(position).getCityName());
			notifyDataSetChanged();
			
			
			
		}
		
	}

}
